<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item :labelCol="labelCol"   :wrapperCol="wrapperCol" label="所属项目" >
          <a-select placeholder='请选择项目' @change="initDepart" :disabled='editDisable' v-decorator="['projectId', {rules:[{required:true,message:'请选择所属项目'}]}]"  :filter-option="filterOption" option-filter-prop="children" show-search>
            <a-select-option v-for="(project,index) in this.projectSelectOption" :key="index" :value="project.id">
              {{project.name}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上级部门">
          <a-select :disabled='editDisable' placeholder="请选择上级部门"  :filter-option="filterOption" option-filter-prop="children" show-search v-decorator="['parentId', {}]">
            <a-select-option value="">
              请选择部门
            </a-select-option>
            <a-select-option v-for="item in this.departmentList" :key="item.id" :value="item.id">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"

          label="部门编号">
          <a-input placeholder="请输入部门编号" v-decorator="['departmentNum', {rules:[{required:true,message:'请输入部门编号'}]}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="请输入部门">
          <a-input placeholder="请输入部门名称" v-decorator="['name',{rules:[{required:true,message:'请输入部门名称'}]}]"/>
        </a-form-item>

        <a-form-item
          v-show="false"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="创建人">
          <a-input placeholder="请输入创建人" v-decorator="['createUser', {}]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "DepartmentModal",
    props: ['projectSelectOption'],
    data () {
      return {
        departmentList:[],
        title:"操作",
        visible: false,
        model: {typeName:""},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        projectId:{rules: [{ required: true, message: '请选择所属项目!' }]},
        },
        editDisable:false,
        url: {
          add: "/department/add",
          edit: "/department/edit",
          getDepartInfo:"/department/getDepartInfo",
        },
        projectList:[],
        typeList:["户主","家人","亲友","租客","办公人员","公司职员","物业员工"],
        statusList:[{label:"启用",value:1},{label:"禁用",value:2}]
      }
    },
    created () {
    },
    methods: {
        selectChange(value) {
            this.queryParam.id = value;
            console.log("change：" + value)
        },
      typeListSelectChange(value,option){
      },
      test(){

      },
      add () {
        this.edit(null);
      },
      initDepart(value){
        getAction(this.url.getDepartInfo,{projectId:value}).then((res)=>{
          debugger
          if (res.success&&res.result!=null){
            this.departmentList=res.result
          }
        })
      },
      edit (record) {
        if (record!=null){
          this.editDisable=true
        }else {
          this.editDisable=false
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'departmentNum','departNum','name','parentId','projectId','typeName','typeAlias','createUser'))
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpUrl = '';
            let method = '';
            if(!this.model.id){
              httpUrl+=this.url.add;
              method = 'post';
            }else{
              httpUrl+=this.url.edit;
               method = 'post';
            }
            let formData = Object.assign(this.model, values);
            formData.createTime = undefined
            httpAction(httpUrl,formData,method).then((res)=>{

              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel () {
        this.close()
      },
      filterOption(input, option) {

        input = String(input)
        return (
          option.componentOptions.children[0].text.toUpperCase().indexOf(input.toUpperCase()) >= 0
        );
      },
    }
  }
</script>

<style lang="less" scoped>

</style>